Ninja Database Pro
AddCompositeIndex<T>(String,List<String>,IndexStyle) Method
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class > AddCompositeIndex Method : AddCompositeIndex<T>(String,List<String>,IndexStyle) Method
A unique name for the index
A list of properties for the composite index
Uniqe or NonUnique
Add a composite index for the type
Syntax
Public Overloads Sub AddCompositeIndex(Of T As {Class, New})( _
   ByVal indexName As String, _
   ByVal properties As List(Of String), _
   ByVal indexStyle As IndexStyle _
) 
Dim instance As NinjaDbPro
Dim indexName As String
Dim properties As List(Of String)
Dim indexStyle As IndexStyle
 
instance.AddCompositeIndex(Of T)(indexName, properties, indexStyle)
public void AddCompositeIndex<T>( 
   string indexName,
   List<string> properties,
   IndexStyle indexStyle
)
where T: class, new()
public:
void AddCompositeIndexgeneric<typename T>
( 
   String^ indexName,
   List<String^>^ properties,
   IndexStyle indexStyle
) 
where T: ref class, gcnew()

Parameters

indexName
A unique name for the index
properties
A list of properties for the composite index
indexStyle
Uniqe or NonUnique

Type Parameters

T
Example
NinjaDbPro db = new NinjaDbPro("CompositeIndexExampleDir", "CompositeIndexExampleDir.db");
 
//Licensed Mode
//db.UserName = "John Smith 101224";
//db.LicenseKey = "aousdf832jasf==";
 
//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
//db.Storage = new MemoryDatabase(); //In memory database
//db.Storage = new FileDatabase();  
 
//Open the database
db.OpenDatabase();
 
//Add a single property index if it doesn't already exist
if (!db.IndexExists<Person>("NameCreatedIndex"))
{
    List<string> indexProperties = new List<string>();
    indexProperties.Add("Name");
    indexProperties.Add("DateCreated");
    db.AddCompositeIndex<Person>("NameCreatedIndex", indexProperties,IndexStyle.NonUnique);
}
 
//Add a record to the database
Person person = new Person();
person.Name = "John Smith";
person.DateCreated = DateTime.Now;
db.Save(person);
 
//Build the index to find.  This has to be in the same order as the index
List<object> indexValues = new List<object>();
indexValues.Add(person.Name);
indexValues.Add(person.DateCreated);
 
//Find an exact matach.  If the index is unique, this is the fastest.
var exactQuery = db.CreateCompositeIndexQuery<Person>("NameCreatedIndex", indexValues);
 
//Lazy load the first record and write out a property
Console.WriteLine(exactQuery[0].LazyValue.DateCreated);
 
//Find a partial match
var partialQuery = db.CreateCompositeIndexQuery<Person>("NameCreatedIndex").Where(o => o.Indexes[0].ToString().StartsWith("John"));
 
//Write the DateCreated for all records that have a name beginning with John
foreach (var indexRecord in partialQuery)
{
    Console.WriteLine(indexRecord.LazyValue.DateCreated);
}
 
//Close the database
db.CloseDatabase();
Dim db As New NinjaDbPro("CompositeIndexExampleDir", "CompositeIndexExampleDir.db")
 
'Licensed Mode
'db.UserName = "John Smith 101224";
'db.LicenseKey = "aousdf832jasf==";
 
'Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
'db.Storage = new MemoryDatabase(); //In memory database
'db.Storage = new FileDatabase();  
 
'Open the database
db.OpenDatabase()
 
'Add a single property index if it doesn't already exist
If Not db.IndexExists(Of Person)("NameCreatedIndex") Then
    Dim indexProperties As New List(Of String)()
    indexProperties.Add("Name")
    indexProperties.Add("DateCreated")
    db.AddCompositeIndex(Of Person)("NameCreatedIndex", indexProperties,IndexStyle.NonUnique)
End If
 
'Add a record to the database
Dim person As New Person()
person.Name = "John Smith"
person.DateCreated = Date.Now
db.Save(person)
 
'Build the index to find.  This has to be in the same order as the index
Dim indexValues As New List(Of Object)()
indexValues.Add(person.Name)
indexValues.Add(person.DateCreated)
 
'Find an exact matach.  If the index is unique, this is the fastest.
Dim exactQuery = db.CreateCompositeIndexQuery(Of Person)("NameCreatedIndex", indexValues)
 
'Lazy load the first record and write out a property
Console.WriteLine(exactQuery(0).LazyValue.DateCreated)
 
'Find a partial match
Dim partialQuery = db.CreateCompositeIndexQuery(Of Person)("NameCreatedIndex").Where(Function(o) o.Indexes(0).ToString().StartsWith("John"))
 
'Write the DateCreated for all records that have a name beginning with John
For Each indexRecord In partialQuery
    Console.WriteLine(indexRecord.LazyValue.DateCreated)
Next indexRecord
 
'Close the database
db.CloseDatabase()
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

NinjaDbPro Class
NinjaDbPro Members
Overload List